package client;

import java.awt.Color;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.GridLayout;
import java.awt.Image;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.BorderFactory;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.SwingUtilities;

/** Costruisce lo splashscreen iniziale del client, permettendo di inserire indirizzo del server,
 * porta e di scegliere tra RMI e Socket.
 * 
 * @author LordMzn
 *
 */
public class ClientLauncher {
	private static final String imagePath = "img/bannUp.png";
	private JFrame frame;
	
	public ClientLauncher() {
		SwingUtilities.invokeLater(new Runnable() {
			@Override public void run() {
				JFrame finestra = new JFrame("Isola dei dinosauri");
				finestra.setLocation(50, 75);
				finestra.setSize(GraphicView.widthMainframe, GraphicView.heigthMainframe-20);
				
				JPanel unico = new JPanel(new FlowLayout(FlowLayout.TRAILING));
				JPanel opzioni =  new JPanel();
				opzioni.setOpaque(false);
				opzioni.setPreferredSize(new Dimension(240,120));
				opzioni.setLayout(new GridLayout(3,2,2,2));
				opzioni.setBorder(BorderFactory.createLineBorder(Color.red, 5));
				opzioni.setBorder(BorderFactory.createTitledBorder(opzioni.getBorder(), "OPZIONI"));
				JLabel indirizzoHost = new JLabel("URL dell'host",JLabel.TRAILING);
				JTextField host =  new JTextField(20);
				JLabel portaHost = new JLabel("Porta",JLabel.TRAILING);
				JTextField port =  new JTextField(20);
				JButton rmi = new JButton("RMI"); rmi.addActionListener(new MyAction(host, port, false));
				JButton socket = new JButton("Socket"); socket.addActionListener(new MyAction(host, port, true));
				opzioni.add(indirizzoHost); opzioni.add(host); 
				opzioni.add(portaHost); opzioni.add(port);
				opzioni.add(rmi); opzioni.add(socket);
				unico.add(opzioni);
				
				ImageIcon sfondo = createImageIcon(imagePath, "sfondo", GraphicView.widthMainframe, GraphicView.heigthMainframe);
				JLabel background = new JLabel(sfondo);
				background.setBounds(0, 0, sfondo.getIconWidth(), sfondo.getIconHeight());
				finestra.getLayeredPane().add(background, new Integer(Integer.MIN_VALUE));
				
				unico.setOpaque(false);
				finestra.setContentPane(unico);
				finestra.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
				finestra.setVisible(true);
				frame = finestra;
			}
		});
	}
	
	public static void main(String[] args){
		new ClientLauncher();
	}
	/** Returns an ImageIcon scaled on width and height parameters,
	 * or null if the path was invalid.
	 **/
	private ImageIcon createImageIcon(String path, String description, int width, int height) {
	    java.net.URL imgURL = getClass().getResource(path);
	    if (imgURL != null) {
	    	ImageIcon myIcon = new ImageIcon(imgURL);
	    	Image img = myIcon.getImage();  
	    	Image newimg = img.getScaledInstance(width, height,  java.awt.Image.SCALE_SMOOTH);  
	        return new ImageIcon(newimg, description);
	    } else {
	        System.err.println("Couldn't find file: " + path);
	        return null;
	    }
	}
	
	class MyAction implements ActionListener {
		private JTextField host;
		private JTextField port;
		private boolean isSocket;
		public MyAction(JTextField h, JTextField p, boolean isS) {
			host = h; port = p; isSocket = isS;
		}
		@Override
		public void actionPerformed(ActionEvent e) {
			String hos;
			int por;
			do {
				hos = host.getText();
				if (isSocket)
					por = Integer.parseInt(port.getText());
				else
					por = 1;
			} while (por<0);					
			new ClientModel(true, isSocket, hos, por);
			frame.dispose();
		}		
	}
}
